package com.ct.controller;

import java.util.List;

import com.ct.utils.MD5Utils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ct.entity.po.User;
import com.ct.entity.query.UserQuery;
import com.ct.entity.vo.CheckCodeVO;
import com.ct.entity.vo.ResponseVO;
import com.ct.service.UserService;
import com.wf.captcha.ArithmeticCaptcha;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;

/**
 * 用户表 Controller
 */
@RestController("userController")
@RequestMapping("/user")
public class UserController extends ABaseController {

    @Resource
    private UserService userService;

    /**
     * 根据条件分页查询
     */
    @RequestMapping("/loadDataList")
    public ResponseVO loadDataList(UserQuery query) {
        return getSuccessResponseVO(userService.findListByPage(query));
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    public ResponseVO add(User bean) {
        userService.add(bean);
        return getSuccessResponseVO(null);
    }

    /**
     * 批量新增
     */
    @RequestMapping("/addBatch")
    public ResponseVO addBatch(@RequestBody List<User> listBean) {
        userService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 批量新增/修改
     */
    @RequestMapping("/addOrUpdateBatch")
    public ResponseVO addOrUpdateBatch(@RequestBody List<User> listBean) {
        userService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据UserId查询对象
     */
    @RequestMapping("/getUserByUserId")
    public ResponseVO getUserByUserId(String userId) {
        return getSuccessResponseVO(userService.getUserByUserId(userId));
    }

    /**
     * 根据UserId修改对象,只能修改电话，email，密码
     */
    @RequestMapping("/updateUserByUserId")
    public ResponseVO updateUserByUserId(@NotEmpty String phone, @NotEmpty String email, @NotEmpty String password,
            @NotEmpty String userId) {
//        User bean = User.builder().build();
//        bean.setPassword(password);
        System.out.println("传入后端的密码是？"+password);
        // 对输入的密码进行 MD5 加密
        String encryptedPassword = MD5Utils.md5(password);
        System.out.println("传到数据库中的加密代码是"+encryptedPassword);
        User bean = User.builder()
                    .password(encryptedPassword)
                    .email(email)
                    .phone(phone)
                    .build();
//        bean.setPassword(encryptedPassword);
//        bean.setEmail(email);
//        bean.setPhone(phone);
        userService.updateUserByUserId(bean, userId);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据UserId删除
     */
    @RequestMapping("/deleteUserByUserId")
    public ResponseVO deleteUserByUserId(String userId) {
        userService.deleteUserByUserId(userId);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/login")
    public ResponseVO login(@NotEmpty String position, @NotEmpty String userId, @NotEmpty String password) {
        return getSuccessResponseVO(userService.login(position, userId, password));
    }

    @PostMapping("/getAllUsersByIdList")
    public List<User> getAllUsersByIdList(@RequestBody List<String> userIdList) {
        return userService.getAllUsersByIdList(userIdList);
    }

    @RequestMapping("/checkCode")
    public ResponseVO checkCode() {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 42);
        String checkCode = captcha.text();

        CheckCodeVO checkCodeVO = new CheckCodeVO();
        checkCodeVO.setCheckCode(checkCode);
        checkCodeVO.setCheckCodeImg(captcha.toBase64());

        return getSuccessResponseVO(checkCodeVO);
    }
}